<template>
	<view class="view-contain">
		<!-- 公共组件-每个页面必须引入 -->
		<public-module leftIcon="arrow-left" :title="title" />
		<u-tabs lineColor="#409EFF" lineWidth="120rpx" :list="[{ name: '我要打分' }]" />
		<view class="form-contain">
			<u-form :rules="rules" labelPosition="left" :model="formInfo" labelWidth="auto" ref="addForm">

				<u-form-item label="评价分数" prop="score" borderBottom>
					<u-input :disabled="type === 3" v-model="formInfo.score"
						:placeholder="type === 3 ? '' : '请输入1-10整数的评价分数'" type="number" border="none"
						@blur="validateScore" />
				</u-form-item>
				<u-form-item label="讲师评价" prop="comment" borderBottom>
					<u-textarea :disabled="type === 3" v-model="formInfo.comment"
						:placeholder="type === 3 ? '' : '请输入至少20字的评价'" />
				</u-form-item>
			</u-form>
		</view>
		<view class="bottom-btn-contain">
			<view style="width: 100%;">
				<u-button v-if="type === 1" type="primary" shape="circle" @click="toAdd">保存评分</u-button>
				<u-button v-if="type === 2" type="primary" shape="circle" @click="toUpdate">确认修改</u-button>
				<u-row v-if="type === 3" gutter="24">
					<u-col span="6">
						<u-button type="error" shape="circle" @click="toDelete">删除评论</u-button>
					</u-col>
					<u-col span="6">
						<u-button type="primary" shape="circle" @click="type = 2">编辑评论</u-button>
					</u-col>
				</u-row>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		evaluationAdd,
		evaluationEdit,
		evaluationDelete
	} from "@/config/api.js"

	export const addFormRules = {
		'comment': {
			required: true,
			min: 20,
			message: '请输入评价内容，至少20字',
			trigger: ['blur', 'change']
		}
	}

	export default {
		data() {
			return {
				rules: addFormRules,
				// 1新增，2编辑，3详情
				type: 1,
				formInfo: {
					id: null,
					userId: null,
					userName:null,
					teacherId: null,
					subject: null,
					score: null,
					comment: null,
					evaluationDate: null,
				},
			}
		},
		computed: {
			...mapState(['user']),
			title() {
				switch (this.type) {
					case 1:
						return '新增点评'
					case 2:
						return '编辑点评'
					case 3:
						return '点评详情'
				}
			}
		},
		onLoad(option) {
			if (option.type == 1) {
				const {
					teacherId,
					subject
				} = option;
				this.formInfo.teacherId = teacherId;
				this.formInfo.subject = subject;
				console.log(teacherId + subject);
				this.type = Number(option.type)
			} else {
				const data = JSON.parse(option.data).shift()
				this.formInfo = data
				this.type = Number(option.type)
			}
		},
		methods: {
			...mapMutations(['setLoginShow', 'emptyUserInfo']),
			confirm(e) {
				this.formInfo.teacherId = this.formInfo.teacherId || null; // 确保有值或者为 null
				this.formInfo.subject = this.formInfo.subject || null;
			},
			validateScore() {
				const score = Number(this.formInfo.score);
				if (isNaN(score)) {
					uni.$u.toast('请输入有效的数字');
					return;
				}
				if (!Number.isInteger(score)) {
					uni.$u.toast('请输入整数');
					return;
				}
				if (score < 1 || score > 10) {
					uni.$u.toast('请输入1 - 10之间的整数作为评价分数');
					return;
				}
			},
			toAdd() {
				this.$refs.addForm.validate().then(async () => {
					const res = await evaluationAdd({
						...this.formInfo,
						userId: this.user.userId,
						userName: this.user.userName
					})
					if (res.code === 200) {
						uni.$u.toast('保存成功')
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 500)
					} else {
						uni.$u.toast(res.msg || '保存失败')
					}
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			toUpdate() {
				this.$refs.addForm.validate().then(async () => {
					const res = await evaluationEdit({
						...this.formInfo,
						userId: this.user.userId,
						userName: this.user.loginName
					})
					if (res) {
						uni.$u.toast('修改成功')
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 500)
					} else {
						this.emptyUserInfo();
						uni.showModal({
							title: '提示',
							content: '当前登录已无效，请重新登陆。',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									uni.navigateBack({
										delta: 1
									});
								}
							}
						});
					}
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			async toDelete() {
				const res = await evaluationDelete(this.formInfo.id)
				if (res) {
					uni.$u.toast('删除成功')
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 500)
				}
			}
		},
		onReady() {
			//this.$refs.addForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss" scoped>
	.view-contain {
		position: relative;
		height: 100vh;
		overflow-y: auto;

		/deep/ .u-checkbox-group {
			flex-wrap: wrap;
		}

		.form-contain {
			background-color: #fff;
			border-radius: 32rpx;
			margin: 12rpx 24rpx;
			padding: 24rpx;

			.title {
				font-weight: bold;
				margin-bottom: 16rpx;
			}

			&.bottom-contain {
				margin-bottom: 200rpx;
			}
		}

		.bottom-btn-contain {
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 99;
			width: calc(100vw - 48rpx);
			height: 150rpx;
			background-color: #fff;
			padding: 48rpx 24rpx 0;
		}
	}
</style>